home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 20 / 2 / DISK2028.ZIP / SEND2LJ.C < prev    next >
C/C++ Source or Header  |  1988-11-02  |  8KB  |  200 lines

  1. /* Utility program to send commands and data to a H-P LaserJet or compatible */
  2. /* printer connected to Centronics parallel port LPTn: for some value of n   */
  3.  
  4. /* Copyright 1988 by Daniel Ross                                             */
  5.  
  6. /* Coded in Microsoft C 5.1 by Daniel Ross, starting 10/31/88                */
  7. /* Revised 11/2/88   13:15                                                   */
  8.  
  9. /* This program sends commands and files of data to the printer.  The files  */
  10. /* may contain either fonts, or text to be printed.                          */
  11.  
  12. /* Input to this program is a command file of ASCII text.  The text is       */
  13. /* organized into records, each record ending with CarriageReturn and        */
  14. /* LineFeed characters.                                                      */
  15.  
  16. /* Each record may specify a single printer command (possibly followed by a  */
  17. /* comment), or the record may name a file to be sent to the printer, or the */
  18. /* record may contain only a comment.                                        */
  19.  
  20. /* Any printing characters at the start of a record are significant.  A      */
  21. /* nonprinting character, such as Space or Tab, terminates the significant   */
  22. /* part of the record.  The remainder of the record is treated as a comment. */
  23. /* Any record that starts with a nonprinting character, is treated as        */
  24. /* comment only.                                                             */
  25.  
  26. /* If the 1st character in the record is a MinusSign, the printing           */
  27. /* characters that follow must name a file (including path, if needed).      */
  28. /* The file name is terminated by any nonprinting character.  This program   */
  29. /* sends the contents of the file to the printer.                            */
  30.  
  31. /* If the 1st character in the record is any printing character except       */
  32. /* MinusSign, the record is treated as a command to the printer.  This       */
  33. /* program sends to the printer an Escape character followed by the          */
  34. /* significant printing characters in the record.  The command is terminated */
  35. /* by any nonprinting character.                                             */
  36.  
  37. /* If your printer is not connected to LPT1:, change the definition of       */
  38. /* PrinterNr (below).                                                        */
  39.  
  40. /* Thanks to David Heintz, Software Consultant, Santa Cruz, CA, for          */
  41. /* debugging help.                                                           */
  42.  
  43. #include <STDLIB.H>
  44. #include <STDIO.H>
  45. #include <CONIO.H>
  46. #include <DOS.H>
  47.  
  48. #define FALSE               0
  49. #define TRUE                1
  50. #define TorF                int
  51.  
  52. #define Escape              0x1B
  53.  
  54. #define PrinterIrp          0x17
  55. #define PrinterNr           0           /* This selects LPT1:                */
  56.  
  57. /* int86 printer commands                                                    */
  58. #define InitPrinterPort     1
  59. #define WriteToPrinter      0
  60. #define ReadPrinterStatus   2
  61.  
  62. /* int86 printer status bits                                                 */
  63. #define Timeout             0x01
  64. #define IOErr               0x08
  65. #define Selected            0x10
  66. #define OutOfPaper          0x20
  67. #define PrinterAck          0x40
  68. #define PrinterReady        0x80
  69.  
  70. char    CmdBuf [256] ;                  /* Buffer for 1 record from the      */
  71.                                         /* command file                      */
  72. char        * CmdFileName ;
  73. FILE        * CmdFileHandle ;
  74. char        * DataFileName   =   & CmdBuf [1] ;
  75. FILE        * DataFileHandle ;
  76. char        * Cursor ;
  77. int         DataChar ;
  78. TorF        SendingFromDataFile ;
  79. long        DataByteNr ;
  80. union REGS  CallRegs, RetRegs ;
  81.  
  82. int   PrintingChar (int GivenByte)
  83.   {
  84.     return ((GivenByte > ' ')   &&   (GivenByte <= '~')) ;
  85.   }
  86.  
  87. void   Send1ByteToPrinter (int GivenByte)
  88.   {
  89.     CallRegs.x.dx   =   PrinterNr ;
  90.     CallRegs.h.ah   =   WriteToPrinter ;
  91.     CallRegs.h.al   =   (char) GivenByte ;
  92.     for (;;)
  93.       {
  94.         int86 (PrinterIrp, & CallRegs, & RetRegs) ;
  95.         if     (RetRegs.h.ah   &   Timeout)                   continue ;
  96.         if ( ! (RetRegs.h.ah   &   (IOErr | OutOfPaper)))     break ;
  97.         printf ("\n\nError writing to the printer") ;
  98.         if (SendingFromDataFile)
  99.           printf
  100.             (
  101.               ",\n   data byte number %ld(decimal) = %lx(hex)"
  102.                 ,
  103.               DataByteNr
  104.                 ,
  105.               DataByteNr
  106.             ) ;
  107.         printf (":  data value = %02x(hex)", GivenByte) ;
  108.         printf ("\nPrinter status = %02x(hex)", ((int) RetRegs.h.ah) & 0xFF) ;
  109.         break ;
  110.       } ;
  111.   }
  112.  
  113. int   main (int argc, char * argv [], char * envp [])
  114.   {
  115.     if (argc != 2)
  116.       {
  117.         printf ("\nExactly 1 parameter needed = name of command file") ;
  118.         exit (1) ;
  119.       } ;
  120.     CmdFileName   =   argv [1] ;
  121.     if ( ! (CmdFileHandle = fopen (CmdFileName, "rt")))
  122.       {
  123.         printf ("\nUnable to open command file %s", CmdFileName) ;
  124.         exit (1) ;
  125.       } ;
  126.     CallRegs.x.dx   =   PrinterNr ;
  127.     CallRegs.h.ah   =   InitPrinterPort ;
  128.     CallRegs.h.al   =   0 ;
  129.     int86 (PrinterIrp, & CallRegs, & RetRegs) ;
  130.     for (;;)
  131.       {
  132.         if ( ! fgets (CmdBuf, 256, CmdFileHandle))
  133.           {
  134.             if (feof (CmdFileHandle))   break ;
  135.             if (ferror (CmdFileHandle))
  136.               {
  137.                 printf ("\nError reading command file %s", CmdFileName) ;
  138.                 exit (1) ;
  139.               } ;
  140.           } ;
  141.         printf ("%s", CmdBuf) ;         /* Show the record                   */
  142.         Cursor = CmdBuf ;
  143.         if ( ! PrintingChar ( * Cursor))   continue ;   /* Comment only      */
  144.         if ( * Cursor   ==   '-')
  145.           {
  146.             /* The record names a file to send to the printer.  Terminate    */
  147.             /* the record after the file name, thereby effectively deleting  */
  148.             /* any comments.                                                 */
  149.             while (PrintingChar (((int) ( * Cursor ++ )) & 0xFF))   ;
  150.             * Cursor   =   0 ;
  151.             if ( ! (DataFileHandle = fopen (DataFileName, "rb")))
  152.               {
  153.                 printf ("\nUnable to open data file %s", DataFileName) ;
  154.                 exit (1) ;
  155.               } ;
  156.             SendingFromDataFile = TRUE ;
  157.             for (DataByteNr = 0 ;   ;   ++ DataByteNr)
  158.               {
  159.                 DataChar   =   fgetc (DataFileHandle) ;
  160.                 if (DataChar == EOF)
  161.                   {
  162.                     if (feof (DataFileHandle))   break ;
  163.                     if (ferror (DataFileHandle))
  164.                       {
  165.                         printf ("\nError reading data file %s", DataFileName) ;
  166.                         exit (1) ;
  167.                       } ;
  168.                   } ;
  169.                 Send1ByteToPrinter (DataChar & 0xFF) ;
  170.               } ;
  171.             if (fclose (DataFileHandle))
  172.               {
  173.                 printf ("\nError closing data file %s", DataFileName) ;
  174.                 exit (1) ;
  175.               } ;
  176.           }
  177.         else
  178.           {
  179.             /* The record contains a printer command                         */
  180.             SendingFromDataFile = FALSE ;
  181.             Send1ByteToPrinter (Escape) ;
  182.             for (;;)
  183.               {
  184.                 DataChar   =   ((int) ( * Cursor ++ )) & 0xFF ;
  185.                 if ( ! PrintingChar (DataChar))   break ;
  186.                 Send1ByteToPrinter (DataChar) ;
  187.               } ;
  188.           } ;
  189.       } ;
  190.     if (fclose (CmdFileHandle))
  191.       {
  192.         printf ("\nError closing command file %s", CmdFileName) ;
  193.         exit (1) ;
  194.       } ;
  195.     exit (0) ;
  196.   }
  197.  
  198. /* End of file                                                               */
  199.  
  200.